package com.android.zweibo.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.zweibo.adapter.UserListAdapter;
import com.android.zweibo.bean.Common;
import com.android.zweibo.bean.Task;
import com.android.zweibo.bean.UserInfo;
import com.android.zweibo.logic.MainService;
import com.android.zweibo.logic.UserInfoServices;
import com.android.zweibo.util.SharedPreferenceUtil;

public class LoginActivity extends Activity implements IWeiboActivity{
	
	private Button account_add_btn;
    private Button login_btn;
    private TextView nowUserName ;
    private ImageView nowUserImage;
    private UserInfoServices userInfoServices;
    private List<UserInfo> userList;
    private ImageButton user_sel_btn;
    private UserInfo nowUser;
    private ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.login);
    	
    	
    	
    	account_add_btn = (Button) this.findViewById(R.id.account_add_btn);
    	login_btn = (Button) this.findViewById(R.id.login_btn);
    	nowUserImage = (ImageView) this.findViewById(R.id.nowuser_image);
    	nowUserName = (TextView) this.findViewById(R.id.nowuser_name);
    	user_sel_btn = (ImageButton) this.findViewById(R.id.user_select_btn);
    	
    	//启动MainService服务
    	Intent mainServiceIntent = new Intent(this,MainService.class);
    	this.startService(mainServiceIntent);
    	
    	MainService.addActivity(this);
        
    	//查看是否存在登录用户
        nowUser = SharedPreferenceUtil.getLoginUser(this);
        
        init();
        
        if(null != nowUser){
        	showDialog();
        	newTask();
        }
    	
    	
    	
    	
    	
    }
    
    /**
     * 显示登录进度窗口
     */
    private void showDialog() {
		if(null == progressDialog) {
			progressDialog = new ProgressDialog(this);
		}
		progressDialog.setMessage("正在登录");
		progressDialog.show();		
	}

    
    
	/**
     * 新建登录任务
     */
    public void newTask(){
    	Map<String,Object> param = new HashMap<String,Object>();
    	param.put("user", nowUser);
		Task t = new Task(Common.USER_LOGIN,param);
	    MainService.newTask(t);	
		}
    
    
    
    /**
     * 初始化
     */
	@Override
	public void init() {
		userInfoServices = new UserInfoServices(this);
	    userList = userInfoServices.findAllUser();
	    
	    if(null == userList || userList.isEmpty()) {
	    	 //打开授权界面
        	Intent intent = new Intent(this,AuthActivity.class);
        	this.startActivity(intent);
        	this.finish();
	    }else {
	    	//添加新帐号
	    	account_add_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(LoginActivity.this,WebViewActivity.class);
					startActivity(intent);
				}
			});
	    	//用户选择
	    	user_sel_btn.setOnClickListener(new UserSelOnClickEvent());
	    	//用户登录
	    	login_btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					showDialog();
					SharedPreferenceUtil.saveLoginUser(LoginActivity.this,nowUser);
					newTask();
				}
			});
	    }			
	}
    
	
	/**
	 * 刷新界面
	 */
	@Override
	public void refresh(Object... objects) {
			progressDialog.dismiss();
			jump();			
	}
	
	
	/**
	 * 跳转到主界面
	 */
	private void jump() {
		Intent intent = new Intent(this,MainActivity.class);
		startActivity(intent);
		this.finish();
	}

	

	/**
	 * 用户选择按钮点击事件
	 * @author administrator
	 *
	 */
	class UserSelOnClickEvent implements OnClickListener {

		@Override
		public void onClick(View view) {
		     View user_sel_view=  View.inflate(LoginActivity.this, R.layout.user_list, null);
		     final Dialog dialog = new Dialog(LoginActivity.this);
		     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		     dialog.setContentView(user_sel_view);
		     dialog.show();
		     
		     ListView listView = (ListView) user_sel_view.findViewById(R.id.users_listview);
		     
		     listView.setAdapter(new UserListAdapter(LoginActivity.this,userList));
		     
		     listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int arg2, long id) {
					ImageView userIcon = (ImageView) view.findViewById(R.id.users_list_icon);
					TextView userName = (TextView) view.findViewById(R.id.users_list_name);
					
					nowUserImage.setImageDrawable(userIcon.getDrawable());
					nowUserName.setText(userName.getText());
					nowUser = userInfoServices.findUserByID(id + "");
					dialog.dismiss();
				}
			});
			
		}
		
	}
}
